<?php

@session_start();

define('AUTH_BASE_URL', 'http://openid.modules4share.com/hotmail/connect');

define('APP_PATH', dirname(dirname(__FILE__)));

set_include_path(implode(PATH_SEPARATOR, array(
    APP_PATH . '/libs',
    get_include_path()
)));

function __autoload($class_name)
{
    $filename = APP_PATH . '/../libs/' . str_replace('_', '/', $class_name) . '.php';
    if (file_exists($filename))
    {
        require_once $filename;
        return true;
    }
    return false;
}

spl_autoload_register("__autoload");

$service = $hashcode = $returnurl = $failedurl = $callbackurl = $securityToken = null;

if (isset($_REQUEST['service']) && $_REQUEST['service'])
{
    $_SESSION['service'] = $service = $_REQUEST['service'];
}
else
{
    $service = isset($_SESSION['service']) ? $_SESSION['service'] : '';
}

$returnurl = isset($_SESSION['returnurl']) ? $_SESSION['returnurl'] : (AUTH_BASE_URL . '/test/response.php');

if (!$service)
{
    // no service select
    exit('bad request, no service choosen!');
}

$platform =  $_SESSION['platform'];

define('AUTH_SERVICE', $service);
define('AUTH_RETURN_URL', $returnurl);
define('AUTH_HASHCODE', $hashcode);
define('AUTH_FAILED_URL', $failedurl);
define('AUTH_CALLBACK_URL', $callbackurl);
define('AUTH_SECURITY_TOKEN', $securityToken);
define('AUTH_PLATFORM', $platform);


function processCentralServiceResponseData($json_data)
{

    $mapper =  new M2b_Adapter_phpfox();
    
    
    
    $bxProfile = $mapper->getProfile(AUTH_SERVICE,$json_data);
    
    $data = json_encode($bxProfile);
    
                
    $data = $sForm = '<form id="dataform" method="post" action="' . AUTH_RETURN_URL . '" >
    <input type="hidden" name="json_data" value= \'' . $data . '\' />
    <input type="hidden" name="hashcode" value="' . AUTH_HASHCODE . '"/>
    <input type="hidden" name="core[security_token]" value="' . AUTH_SECURITY_TOKEN . '"/>
    <input type="hidden" name="service" value="' . AUTH_SERVICE . '" />
    <input type="hidden" name="domain" value="' . $_SERVER['HTTP_HOST'] . '"/>
    </form>
    <script type="text/javascript">document.getElementById("dataform").submit();</script>
    ';
    echo $sForm;
    exit ;
}

function processCentralServiceResponseDataGet($user){
    
    $return_url =  AUTH_RETURN_URL;
    
    $mapper = null;
     $mapper =  new M2b_Adapter_phpfox();
    switch(AUTH_PLATFORM){
        case 'phpfox':
            $mapper =  new M2b_Adapter_phpfox();
            break;
        case 'socialengine4': default:
            $mapper =  new M2b_Adapter_Se4();
    }
    
    
    $bxProfile = $mapper->getProfile(AUTH_SERVICE,$user);
    
    $res = array();
    
    $res['hashcode'] = AUTH_HASHCODE;
    $res['service'] =  AUTH_SERVICE;
    
        
    foreach($bxProfile as $key=>$value){
        $res[$key] = $value;
    }
    
    if(DEBUG == 2){
        print_r($bxProfile);
        exit();
    }
    
    $ask = '?';
    
    if(strpos($return_url,'?')){
        $ask = '&'; 
    }
    
    if($service =='facebook'){
           if(count($_COOKIE)>0)
          {
                             foreach($_COOKIE as $key=>$value){
             setcookie($key, false, time() - 3600);
            
             }
            unset($_COOKIE);    
          }     
           ;   
    }
    
    if($_SERVER['REMOTE_ADDR'] ==''){
    }
    
    $url = AUTH_RETURN_URL.$ask.http_build_query($res,null,'&');
    
    
    header('location: '.$url);
    
    exit;
}
